<template>
  <div style="height: 900px;" class="personBackground">
    <div>
      <el-form :model="data.user" label-width="100px" style="width: 60%; margin: auto auto;">
        <div style="margin: 20px 0; text-align: center">
          <el-upload :show-file-list="false" class="avatar-uploader" :action="uploadUrl" :on-success="handleFileUpload">
            <img v-if="data.user.avatar" :src="data.user.avatar" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </div>
        <el-form-item label="账号">
          <el-input disabled v-model="data.user.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="data.user.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input disabled v-model="data.user.telephone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="原密码">
          <el-input v-model="data.password" show-password />
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="data.user.newPassword" show-password />
        </el-form-item>
        <el-form-item label="确认新密码">
          <el-input v-model="data.user.confirmPasword" show-password />
        </el-form-item>
        <div style="text-align: center;">
          <el-button type="info" @click="reset">重置</el-button>
          <el-button type="primary" @click="save">保存</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {reactive} from "vue"
import request from "@/utils/request";
import {ElMessage} from "element-plus";
import router from "@/router";

// 文件上传的接口地址
const uploadUrl = import.meta.env.VITE_BASE_URL + '/files/upload'

const data = reactive({
  user: JSON.parse(localStorage.getItem('system-user') || '{}'),
  password: '',
})

const handleFileUpload = (file) => {
  data.user.avatar = file.data
}

const emit = defineEmits(["updateUser"])

const reset = () => {
  data.user = JSON.parse(localStorage.getItem('system-user') || '{}')
}

// 把当前修改的用户信息存储到后台数据库
const save = () => {
  if(data.user.newPassword && data.user.confirmPasword){
    if(data.password !== data.user.password){
      ElMessage.error('输入的原密码不正确')
      return
    }else if(data.user.newPassword === data.user.password){
      ElMessage.error('新密码与原密码一致')
      return
    }else if(data.user.newPassword !== data.user.confirmPasword){
      ElMessage.error('确认密码与新密码不一致')
      return
    }
    data.user.password = data.password
    if (data.user.role === 'ADMIN') {
      request.put('/admin/update', data.user).then(res => {
        if (res.code === '200') {
          ElMessage.success('个人资料修改成功')
          //把更新后的用户信息存储到缓存
          localStorage.setItem('system-user', JSON.stringify(data.user))
          emit('updateUser')
          // 清空缓存
          localStorage.removeItem('system-user')
          router.push('/login')
        } else {
          ElMessage.error(res.msg)
        }
      })
    } else if (data.user.role === 'USER') {
      request.put('/user/update', data.user).then(res => {
        if (res.code === '200') {
          ElMessage.success('个人资料修改成功')
          //把更新后的用户信息存储到缓存
          localStorage.setItem('system-user', JSON.stringify(data.user))
          emit('updateUser')
          // 清空缓存
          localStorage.removeItem('system-user')
          router.push('/login')
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
  }else{
    if (data.user.role === 'ADMIN') {
      request.put('/admin/update', data.user).then(res => {
        if (res.code === '200') {
          ElMessage.success('个人资料修改成功')
          //把更新后的用户信息存储到缓存
          localStorage.setItem('system-user', JSON.stringify(data.user))
          emit('updateUser')
        } else {
          ElMessage.error(res.msg)
        }
      })
    } else if (data.user.role === 'USER') {
      request.put('/user/update', data.user).then(res => {
        if (res.code === '200') {
          ElMessage.success('个人资料修改成功')
          //把更新后的用户信息存储到缓存
          localStorage.setItem('system-user', JSON.stringify(data.user))
          emit('updateUser')
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
  }
}

</script>

<style scoped>
.avatar-uploader .avatar {
  width: 180px;
  height: 180px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px solid var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 40px;
  color: #8c939d;
  width: 180px;
  height: 180px;
  text-align: center;
  background-color: white;
}

.personBackground {
  padding: 60px;
  background-image: url("@/assets/imgs/house.jpg");
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

.el-form-item label {
  color: white;
  height: 55px;
  line-height: 55px;
  font-size: 17px;
}

.el-input {
  height: 40px;
}

.el-button {
  width: 70px;
  height: 40px;
  opacity: 0.9;
}
</style>